package com.jzo2o.orders.manager.controller.consumer;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.jzo2o.common.constants.UserType;
import com.jzo2o.orders.base.model.domain.Orders;
import com.jzo2o.orders.manager.model.dto.OrderCancelDTO;
import com.jzo2o.orders.manager.service.IOrdersCreateService;
import com.jzo2o.orders.manager.service.IOrdersManagerService;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.elasticsearch.common.recycler.Recycler;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 11394
 * @version 1.0
 * @description: TODO
 * @date 2024/8/29 22:43
 */
@Component
public class OrdersHandler {

    @Resource
    IOrdersCreateService ordersCreateService;
    @Resource
    IOrdersManagerService ordersManagerService;
    /**
    * @description
    * @author Gaosir
    * @return * @return: null
    * 支付超过取消订单
     * 每分钟执行一次
    * @date 2024/8/29 22:44
    */
    @XxlJob(value = "canceloverTimepayorder")
    public void cancel(){

        //查询支付超时状态订单
        List<Orders> orders = ordersCreateService.queryoverTimepageOrderlistBycount(100);
        if(CollUtil.isEmpty(orders)){
            XxlJobHelper.log("查询超时订单列表为空");
            return;
        }

        for (Orders order : orders) {
            //取消订单
            OrderCancelDTO orderCancelDTO = BeanUtil.toBean(order, OrderCancelDTO.class);
            orderCancelDTO.setCurrentUserType(UserType.SYSTEM);
            orderCancelDTO.setCancelReason("订单超时支付，自动取消");
            ordersManagerService.cancel(orderCancelDTO);
        }
    }

}
